package com.xiangge.supermarket.dao.impl;

import com.xiangge.supermarket.entity.Member;
import com.xiangge.supermarket.service.SupermarketService;

import java.util.ArrayList;

public class DaoImpl implements SupermarketService {
    // 准备一个动态数组来存放信息
    public static final ArrayList<Member> MEM_LIST = new ArrayList<>();

    // 初始化最开始的几条信息，static静态代码块，程序运行就会输出
    static {
        MEM_LIST.add(new Member("1001","蔡徐坤","铂金卡",2000,"合法账户"));
        MEM_LIST.add(new Member("1002","范建强","钻石卡",5000,"被禁账户"));
        MEM_LIST.add(new Member("1003","高启强","金卡",500,"合法账户"));
    }

    @Override
    public boolean add(Member member) {
        boolean add = MEM_LIST.add(member);
        return add;
    }

    @Override
    public boolean delete(String id) {
        int memIndex;
        memIndex = findById(id);
        if (memIndex == -1){
            return false;
        }
        MEM_LIST.remove(memIndex);
        return true;
    }

    @Override
    public boolean update(Member member) {
        int memIndex = findById(member.getId());
        if (memIndex == -1){
            System.out.println("你要修改的会员不存在！");
            return false;
        }
        MEM_LIST.set(memIndex,member);
        System.out.println("修改会员成功！");
        return true;
    }

    @Override
    public int findById(String id) {
        int index = -1;
        for (int i = 0; i < MEM_LIST.size(); i++) {
            Member member = MEM_LIST.get(i);
            if (member.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    @Override
    public void findAll() {
        if (MEM_LIST.size() == 0){
            System.out.println("请先添加会员信息再查询！");
            return;
        }
        System.out.println("用户ID\t账号\t卡类型\t积分\t卡片状态");
        for (Member member : MEM_LIST) {
            System.out.println(""+member.getId()+"\t"+member.getName()+"\t"+member.getType()+"\t"+member.getIntegration()+"\t"+member.getState());
        }
    }
}
